/********************************************************************
 * Copyright(c) 2023-2023 Grayson Zheng.
 * File: key_4_by_4.C
 * Description: 4 by 4 matrix keyboard program.
 * Version: 1.0
 * Date: 2023-07-21
 * Author: zhengxinyu13@qq.com
 * ---------- Revision History ----------
 * <version>
 * <date>
 * <author>
 * <desc>
 * Modified to be suitable to the new coding rules in all functions.
 ********************************************************************/

#include "key_4_by_4.h"

uint_8 scan()
{
	uint_8 row = 0;
	uint_8 column = 0;

	KEYBOARD_16 = 0x0f;

	if (KEYBOARD_16 != 0x0f) {
		delay_xms(5);
		if (KEYBOARD_16 != 0x0f) {
			row = KEYBOARD_16 & 0x0f;
			KEYBOARD_16 = 0xf0;  
			column = KEYBOARD_16 & 0xf0;
			return (column + row);
		}
	}
	return (0xff);
}
 
uint_8 keyboard_4_by_4()
{
	uint_8 keyboard = 0;
	enum button key = KEY_INVALID;
	keyboard = scan();
	switch (keyboard) {
		case KEYBOARD_ZERO:
			key = KEY_0;
			break;
        case KEYBOARD_ONE:
			key = KEY_1;
			break;
		case KEYBOARD_TWO:
			key = KEY_2;
			break;
		case KEYBOARD_THREE:
			key = KEY_3;
			break;
		case KEYBOARD_FOUR:
			key = KEY_4;
			break;
		case KEYBOARD_FIVE:
			key = KEY_5;
			break;
		case KEYBOARD_SIX:
			key = KEY_6;
			break;
		case KEYBOARD_SEVEN:
			key = KEY_7;
			break;
		case KEYBOARD_EIGHT:
			key = KEY_8;
			break;
		case KEYBOARD_NINE:
			key = KEY_9;
			break;
		case KEYBOARD_ASTERISK:
			key = KEY_ASTERISK;
			break;
		case KEYBOARD_HASH:
			key = KEY_HASH;
			break;
		case KEYBOARD_A:
			key = KEY_A;
			break;
		case KEYBOARD_B:
			key = KEY_B;
			break;
		case KEYBOARD_C:
			key = KEY_C;
			break;
		case KEYBOARD_D:
			key = KEY_D;
			break;
		default:
			key = KEY_INVALID;
			break;
	}
	return key;
}
